<?php
class base extends Controller{

	public function init(){
		parent::init();

		$this->admin_id = $_SESSION['company_user']['id'];
		// $this->mnm = get_cookie('m_user');

		$this->m_user = get_cookie('m_user');
        $this->m_pass = get_cookie('m_pass');
        $this->remember = get_cookie('remember');

        $this->module = arg('m')??'index';
		$this->controller = arg('c');
		$this->action = arg('a');
		$this->routeUrl = $this->module.'/'.$this->controller;
		//
		$this->fmdo = arg('do');
		$this->tab = arg('tab');
		$this->doids = arg('doids');
		$this->keyword = arg('keyword');
		$this->sort = arg('sort');
		$this->list_order = arg('list_order');
		$this->time_s = arg('time_s');
		$this->time_e = arg('time_e');
		$this->status = arg('status');

		$this->sexRadio = array('1'=>'男','0'=>'女');

		$this->moshiRadio = array('1'=>'自由接单','2'=>'自动派单','3'=>'循环派单');
		$this->userLevels = array('1'=>'报修员','2'=>'报修组长','3'=>'维修师傅','4'=>'维修组长','5'=>'科室护士长','6'=>'科室主任');
		$this->orderStatus = array('1'=>'新报修','2'=>'已派单','3'=>'已接单','4'=>'维修中','5'=>'已完成','6'=>'已评价','7'=>'已结算','99'=>'已取消');


		$this->com_id = arg('com_id');
		if($this->com_id){
			$_SESSION['com_id'] = $this->com_id;
			$this->company = D('company')->findById($this->com_id);
		}else{
			$this->com_id = 10007;
			$_SESSION['com_id'] = $this->com_id;
			$this->company = D('company')->findById($this->com_id);
		}

		if($this->admin_id){

			// $this->user = D('company_user')->findById($this->admin_id);
			$this->admin = D('company_admin')->findById($this->admin_id);

			$this->company = D('company')->findById($this->admin['company_id']);
			$this->company_setting = unserialize($this->company['setting']);
			// $this->store = D('company_store')->findById($this->admin['store_id']);
			// $this->site = D('site')->findById($this->company['site_id']);
			// $this->cond_city = " and city_id={$this->site['area_id']}";

			$this->cond_base = " and company_id={$this->company['id']}";

			if($this->admin['area_ids']){
				$this->cond_area = " and area_id IN ({$this->admin['area_ids']})";
			}
			if($this->admin['cate_ids']){
				$this->cond_cate = " and care_id IN ({$this->admin['cate_ids']})";
			}
			if($this->admin['zone_ids']){
				$this->cond_zone = " and zone_id IN ({$this->admin['zone_ids']})";
			}
			if($this->admin['diaocha_ids']){
				$this->cond_diaocha = " and diaocha_id IN ({$this->admin['diaocha_ids']})";
			}

			$this->user = D('company_user')->findById($this->admin['user_id']);
			$this->sys_user = D('sys_user')->findById($this->user['wxuser_id']);
		}

		$this->setting = D('sys_setting')->get_setting();
		$this->upload_url = D('sys_setting')->get_upload_url();

		$this->site_title = $this->setting['site']['site_name'];

		$from = arg('from');
		$this->checkLogin($from);

	}

	public function checkLogin($from){
		// dump($this->routeUrl);exit;
		if($this->routeUrl != $this->module."/login"){
			if(!$this->admin_id){
				if(isWx()){
					$this->redirect("/{$this->module}/login/wxlogin?from={$from}");
				}else{
					$this->redirect("/{$this->module}/login/index");
				}
			 }
		 }
	}

	public function checkUser_api(){
		$user_id = arg('user_id');
		$user_token = arg('user_token');
		if($user_token){
			$user = D('user')->get_user($user_token);
			if($user){
				$admin = D('company_admin')->get_user($user['id']);
				if($admin){
					$_SESSION["company_admin"] = $admin;
					D('company_admin')->up_time($admin['id']);
					$this->admin_id = $admin['id'];

				}
			}
		}
	}

	public function redirect($url){
		$arr = explode('/',$url);
    	if(count($arr) == 3){
			$url = "/{$this->module}{$url}";
		}
		// dump($url);exit;
		header("Location:" . $url);
		exit;
	}

	protected function renderJson($code = 1, $msg = '', $url = '', $data = [])
    {
        header('Content-Type:application/json; charset=utf-8');
        exit(json_encode(compact('code', 'msg', 'url', 'data')));
        // return compact('code', 'msg', 'url', 'data');
    }

    protected function renderSuccess($msg = 'success', $url = '', $data = [])
    {
        return $this->renderJson(1, $msg, $url, $data);
    }

    protected function renderError($msg = 'error', $url = '', $data = [])
    {
        return $this->renderJson(0, $msg, $url, $data);
    }

    protected function postData($key)
    {
        return arg($key);
    }

    protected function isAjax(){
	  if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
	    return true;
	  }else{
	    return false;
	  }
	}

	public function ajaxMsg($msg,$code=0,$link='') {
		// 返回JSON数据格式到客户端 包含状态信息
		header('Content-Type:application/json; charset=utf-8');
        $data['code'] = $code;
        $data['result'] = $msg;
        $data['link'] = $link;
        exit(json_encode($data));
    }


	//cookie
    public function set_cookie($name,$value) {
        $cookie= \lib\PHPCookie::getInstance();
        return $cookie->set($name,$value);
    }
    public function get_cookie($name) {
        $cookie=\lib\PHPCookie::getInstance();
        return $cookie->get($name);
    }
    public function del_cookie($name) {
        $cookie=\lib\PHPCookie::getInstance();
        return $cookie->delete($name);
    }

}